BUILDING = {}
BUILDING.DEBUG = false
COMMVOTE = {}
COMMVOTE.WaitTime = 35.0

include("list.lua")

local function CommVotesChanged( ply )
	hook.Call("CommVotes", GAMEMODE, ply )
end

RegisterNWTablePlayer({
	{"CommVotes", 0, NWTYPE_CHAR, CommVotesChanged, REPL_TEAMONLY }
})

function COMMVOTE:TimeLeft()
	return GAMEMODE:StartTime() + COMMVOTE.WaitTime - CurTime() 
end

function BUILDING:Get( id )
	return self.List[ id ]
end

function BUILDING:UpdatePos( Trace, ent, ply, extra )

	local angle = Trace.HitNormal:Angle()
	ply = ply or LocalPlayer()
	
	angle:RotateAroundAxis( angle:Right(), -90 )
	angle:RotateAroundAxis( angle:Up(), ply:GetAngles().y + (extra or 0) )
	
	ent:SetPos( Trace.HitPos )
	ent:SetAngles( angle )	

end

function BUILDING:GetTrace( ent )

	local mins = ent:OBBMins() * 0.99
	local maxs = ent:OBBMaxs() * 0.99
	local Up = ent:GetUp() * math.abs( mins.z )
	local Pos = ent:GetPos()
	local Angles = ent:GetAngles()
	
	mins:Rotate( Angles )
	maxs:Rotate( Angles )
	
	OrderVectors( mins, maxs )

	return util.TraceHull( {
		mins = mins,
		maxs = maxs,
		start = Pos + Up,
		endpos = Pos + Up * 2,
		filter = ent
	} )
end

function BUILDING:CanSpawn( ent )

	if CLIENT && GHOST.NextSpawn > CurTime() then
		return false
	end
	
	local trace = self:GetTrace( ent )
	
	return trace.Hit == false && !IsValid( trace.Entity )
	
end

function BUILDING:PlayerTrace( ply )
	
	return util.QuickTrace(
		ply:GetShootPos(),
		ply:GetAimVector() * 256,
		ply
	)

end

local meta = FindMetaTable( "Player" )
if (!meta) then 
    Msg("ALERT! Could not hook Player Meta Table\n")
    return 
end

function meta:IsComm()
	return  self:Class():GetName() == "Commander"
end